package com.huanletao.spring.data;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.huanletao.json.JacksonUtils;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class demo5 {
    private static Logger log = LoggerFactory.getLogger(demo5.class);

    public static void main(String[] args) throws Exception {

//
//        HashMap<String, Object> map = Maps.newHashMap();
//        map.put("username","黄剑锋");
//        map.put("phoneNumber","18207700421");
//        String s = JacksonUtils.toJSONString(map);
//        System.out.println(s);
//



//        Person person = new Person("John", 30);
//        Map<String, Object> map = Maps.toMap(person, p -> ImmutableMap.of("name", p.getName(), "age", p.getAge()));
    }

    //获取网页数据公共方法
    public static String util0(String uri) throws Exception {
        System.out.println(uri);
        //创建httpClient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建httpGet实例
        HttpGet httpGet = new HttpGet(uri);
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
        CloseableHttpResponse response = httpClient.execute(httpGet);
        String result = "";
        if (response != null) {
            HttpEntity entity = response.getEntity();  //获取网页内容
            result = EntityUtils.toString(entity, "UTF-8");
            log.info("util0:{}", result);
            if (response != null) {
                response.close();
            }
            if (httpClient != null) {
                httpClient.close();
            }
        }
        return result;
    }
}
class Person {
    private String name;
    private int age;

    public Person(String john, int i) {
        this.name = john;
        this.age = i;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // getters and setters
}